/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.utility.controller;

import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.utility.entity.Call;
import com.ipcc.utility.model.CallModel;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class CallController implements Serializable {

    /**
     * Creates a new instance of CallController
     */
    private String soGoi = "";
    private String soNhan = "";
    private String NgaygioGoi;
    private String ThoigianKetthuc;
    private int thoiluong;
    private String Ghiam = "";
    private Date tuNgay;
    private Date denNgay;
    private List<Call> items = null;
    private String linkAudio = "";
//    private StreamedContent file;
    private LazyDataModel<Call> lazyModel;

    public CallController() {
        items = new ArrayList<Call>();
    }

//    public StreamedContent getFile() {
//        return file;
//    }
//
//    public void setFile(StreamedContent file) {
//        this.file = file;
//    }

//    public StreamedContent downloadFile(String linkAudio) {
//        String separator = System.getProperty("file.separator");
//        InputStream stream = null;
//        try {                        
//            ResourceBundle resource = ResourceBundle.getBundle("config");
//            String urlMedia =  resource.getString("urlMedia");            
//            URL url = new URL(urlMedia +"/"+linkAudio);
//            stream = url.openStream();
//            
//            file = new DefaultStreamedContent(stream, "audio/x-wav", linkAudio);                        
//            return file;
//        } catch (Exception e) {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
//            e.printStackTrace();
//        }
//        finally{            
//            try {
//                stream.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//                Logger.getLogger(CallController.class.getName()).log(Level.SEVERE, null, ex);
//                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ex.getMessage()));
//            }
//        }
//        return null;
//    }

    public LazyDataModel<Call> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<Call> lazyModel) {
        this.lazyModel = lazyModel;
    }
    
    public String getLinkAudio() {
        return linkAudio;
    }

    public void setLinkAudio(String linkAudio) {
        this.linkAudio = linkAudio;
    }   
    
    public List<Call> getItems() {
        return items;
    }

    public void setItems(List<Call> items) {
        this.items = items;
    }

    public String getSoGoi() {
        return soGoi;
    }

    public void setSoGoi(String soGoi) {
        this.soGoi = soGoi;
    }

    public String getSoNhan() {
        return soNhan;
    }

    public void setSoNhan(String soNhan) {
        this.soNhan = soNhan;
    }

    public String getNgaygioGoi() {
        return NgaygioGoi;
    }

    public void setNgaygioGoi(String NgaygioGoi) {
        this.NgaygioGoi = NgaygioGoi;
    }

    public String getThoigianKetthuc() {
        return ThoigianKetthuc;
    }

    public void setThoigianKetthuc(String ThoigianKetthuc) {
        this.ThoigianKetthuc = ThoigianKetthuc;
    }

    public int getThoiluong() {
        return thoiluong;
    }

    public void setThoiluong(int thoiluong) {
        this.thoiluong = thoiluong;
    }   

    public String getGhiam() {
        return Ghiam;
    }

    public void setGhiam(String Ghiam) {
        this.Ghiam = Ghiam;
    }

    public Date getTuNgay() {
        return tuNgay;
    }

    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    public Date getDenNgay() {
        return denNgay;
    }

    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }
    // ham de mo dialog Listen
    public String listen(String link){
        linkAudio = link;
        return "";
    }
    
    
    // ham kiem tra cac thong tin tim kiem
    public boolean checkInput() {
        if (tuNgay != null && denNgay != null && denNgay.compareTo(tuNgay) < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Từ ngày và đến ngày nhập không hợp lệ!"));
            return false;
        }                
        return true;
    }

    public String tim() {
        String format = "dd-MM-yyyy";
        if(!checkInput()) return "";
        try {
            items = CallModel.searchListCallByCondition(soGoi, soNhan, convertDateToString(tuNgay, format), convertDateToString(denNgay, format));
            lazyModel = new LazyAbstractDataModel<Call>(items);
            int i = items.size();
            if (i > 0) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(i + " kết quả được tìm thấy!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không kết quả nào được tìm thấy!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
        }
        return "";
    }
    
    public static String convertDateToString(Date date, String format) {
        if(date == null) return "";        
        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        return mySimpleDateFormat.format(date);
    }
    
    public String makeTime(int seconds) {
        int day = (int) TimeUnit.SECONDS.toDays(seconds);
        long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
        long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);
        long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);

        String time = String.format("%02d:%02d:%02d", hours, minute, second);
        return time;
    }
//    public static void main(String[] args) {
//        StreamedContent file;
//        String separator = System.getProperty("file.separator");
//        try {                        
//            ResourceBundle resource = ResourceBundle.getBundle("config");
//            String urlMedia =  resource.getString("urlMedia");
//            URL url = new URL("http://3one.vn:8686/media/2013-10-26-11-13-49_1202_1203.mp3");            
//            InputStream stream = url.openStream();
//            file = new DefaultStreamedContent(stream, "audio/mepg", "thang.wav");    
//            System.out.println("***Ok");
//        } catch (Exception e) {
////            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
//            e.printStackTrace();
//        }
//    }
}
